Let's look at your original problem and see if we can find some clues.
You say it "works fine for numbers like 111 (7)", but what about another three-bit number?
Binary 101 (5) also produces 7. In fact, any three bit number seems to produce 7. What is 7 in binary? It is 111.
Any four bit number seems to always produce 15. What is 15 in binary? 1111.
Now we've identified a pattern. It seems your code is always behaving as if the input was all ones.
So let's put a print statement in your second "for()" loop to print out the value of "bin[x]" to see what values it holds.
Well! All the elements seem to be one, regardless of what we entered! So maybe the problem isn't with line 19 after all. Let's check out the input on line 13.
Code:
bin[x]=scanf("%d", &bin[x]);
This looks suspicious. Normally when we use "scanf()" to read in an integer, it would look like this:
Code:
scanf("%d", &bin[x]);
The "&binx[x]" tells "scanf()" where to store the input. However, your version has "bin[x]=scanf..." So you're assigning the return value of "scanf()" to "bin[x]", overwriting the value you just read. What is the return value of "scanf()"?
scanf()
So "scanf()" returns the number of items it successfully read. In your case, entering
one integer at a time as expected, "scanf()" returns that number of items (
one) successfully read.
That's where your array is being filled with all ones!
The solution, as stahta01 pointed out in post #2, is to change that line to: scanf("%d", &bin[x]);
(i.e. get rid of the "bin[x]=" part).
And that, my friend, is how we troubleshoot.